import {messages} from "share/common";
import React, { Component } from 'react';
import ExpirationDate from './expiration-date';
import { Col, Radio, Row, Input, InputNumber } from 'antd';

class ExpirationRadio extends Component {
  constructor(props) {
    super(props);
  }

  handleRadio = (e) => {
    let { value: { dynamicRuleFailureTime } } = this.props;
    if (!e.target.value && !dynamicRuleFailureTime) {
      dynamicRuleFailureTime = '12-31';
    }
    this.handleSync({ staticCalibrationFlag: e.target.value, dynamicRuleFailureTime });
  };

  HandleExpiryDay = (e) => {
    this.handleSync({ expiryDay: e });
  };
  handleLocationDate = (dynamicRuleFailureTime) => {
    this.handleSync({ dynamicRuleFailureTime });
  };

  handleSync = (target) => {
    const { onChange, value } = this.props;
    onChange({ ...value, ...target });
  };

  render() {
    let { value } = this.props;
    value || (value = {});
    return (
      <section className="expiration-radio">
        <Radio.Group value={value.staticCalibrationFlag} onChange={this.handleRadio}>
          <Row>
            <Col span={6}>
              <Radio value={true}>
                {messages('supplier.key448')/*静态校验规则：*/}
              </Radio>
            </Col>
            <Col span={18} className={value.staticCalibrationFlag ? '' : 'expiration-radio-unselected'}>
              <span className="expiration-before-desc">{messages('supplier.key449')/*开票后*/}</span>
              <InputNumber
              min={1}
              step={1}
              precision={0}
              disabled={!value.staticCalibrationFlag}
              className='particular-input-width'
              value={value.expiryDay}
              onChange={this.HandleExpiryDay}/>
              <span>{messages('supplier.key450')/*天过期*/}</span>
            </Col>
          </Row>
          <Row>
            <Col span={6}>
              <Radio value={false}>
                {messages('supplier.key451')/*动态校验规则：*/}
              </Radio>
            </Col>
            <Col span={18} className={value.staticCalibrationFlag ? 'expiration-radio-unselected' : ''}>
              <span className="expiration-before-desc">{messages('supplier.key452')/*开票日期早于去年*/}</span>
                <ExpirationDate
                  disabled={value.staticCalibrationFlag}
                  value={value.dynamicRuleFailureTime}
                  onChange={this.handleLocationDate}/>
              <span>{messages('supplier.key453')/*(含此日）则过期*/}</span>
            </Col>
          </Row>
        </Radio.Group>
      </section>
    );
  }
}

ExpirationRadio.propTypes = {
  value: React.PropTypes.object,
  onChange: React.PropTypes.func
};

export default ExpirationRadio;
